package com.rainbowred.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rainbowred.common.enums.ErrorCode;
import com.rainbowred.common.pojo.CommonResult;
import com.rainbowred.common.controller.BaseController;
import com.rainbowred.system.domain.EdpDeveloper;
import com.rainbowred.system.service.EdpDeveloperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Desc:
 * @Date: 2023/10/29 16:55
 * @Author: pengweibiao
 **/
@RestController
@RequestMapping("/developer")
public class EdpDeveloperController extends BaseController {

    @Autowired
    private EdpDeveloperService service;

    @PostMapping("")
    public CommonResult<EdpDeveloper> addOne(@RequestBody EdpDeveloper pojo) {
        return success(service.addOne(pojo));
    }

    @GetMapping("/list")
    public CommonResult<List<EdpDeveloper>> getList() {
        QueryWrapper<EdpDeveloper> qw = new QueryWrapper<>();
        qw.eq("del_flag", 0).orderBy(true, false, "create_date");
        return success(service.list(qw));
    }

//    @RequestMapping("/update/{field}/{id}")
//    public CommonResult<Boolean> disablePojo(@PathVariable(value = "field") String field, @PathVariable(value = "id") String id) {
//        EdpDeveloper pojo = service.getById(id);
//        if (Objects.nonNull(pojo)) {
//            switch (field) {
//                case "disable":
//                    if (pojo.getDisable().equals(1)) {
//                        pojo.setDisable(0);
//                    }else {
//                        pojo.setDisable(1);
//                    }
//                    break;
//                case "del_flag":
//                    pojo.setDelFlag(1);
//                    break;
//                default:
//                    return error("接口不存在");
//            }
//            return result(service.updateById(pojo));
//        }else {
//            return error("数据不存在");
//        }
//    }

    @DeleteMapping("")
    public CommonResult<EdpDeveloper> delOne(@RequestBody EdpDeveloper pojo) {
        UpdateWrapper<EdpDeveloper> qw = new UpdateWrapper<>();
        qw.eq("dev_id", pojo.getDevId())
                .set("disable", 1).set("del_flag", 1);
        return service.update(qw) ? success("删除成功") : error(ErrorCode.E2001, "删除失败");
    }

    @DeleteMapping("/remove")
    public CommonResult<EdpDeveloper> removeOne(@RequestBody EdpDeveloper pojo) {
        QueryWrapper<EdpDeveloper> qw = new QueryWrapper<>();
        qw.eq("dev_id", pojo.getDevId());
        return service.remove(qw) ? success("删除成功") : error(ErrorCode.E2001, "删除失败");
    }

}
